# -*- encoding: utf-8 -*-
#
# Lúcuma
# Copyright © 2008 Diego Escalante Urrelo  <diegoe@gnome.org>
#
# Licensed under the GNU General Public License Version 2
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor
# Boston, MA  02110-1301, USA.
#

import datetime

from django.shortcuts import render_to_response

from lucuma.settings import MEDIA_URL
from lucuma.tasks.models import Task
from lucuma.tasks.forms import EditTaskForm
from lucuma.notes.models import Note
from lucuma.personas.models import Persona

def main(request, done=False):
    """
    List all the available Tasks by due date.
    """
    today = datetime.date.today()
    tomorrow = today + datetime.timedelta(days=1)
    nextweek = today + datetime.timedelta(weeks=1)
    nextnextweek = today + datetime.timedelta(weeks=2)

    overdue_tasks = Task.objects.filter(
        done__exact=done, due__lt=today)
    
    today_tasks = Task.objects.filter(
        done__exact=done, due__exact=today)
    
    tomorrow_tasks = Task.objects.filter(
        done__exact=done, due__exact=tomorrow)
    
    thisweek_tasks = Task.objects.filter(
        done__exact=done, due__lte=nextweek, due__gt=tomorrow)
    
    nextweek_tasks = Task.objects.filter(
        done__exact=done, due__gt=nextweek, due__lte=nextnextweek)
    
    later_tasks = Task.objects.filter(
        done__exact=done, due__gt=nextnextweek).order_by('due')
    
    lastmonth_tasks = None
    
    return render_to_response('tasks_list.html',
        {   'media_url': MEDIA_URL,
            'overdue_tasks': overdue_tasks, 
            'today_tasks': today_tasks, 
            'tomorrow_tasks': tomorrow_tasks, 
            'thisweek_tasks': thisweek_tasks, 
            'nextweek_tasks': nextweek_tasks, 
            'later_tasks': later_tasks, 
            'lastmonth_tasks': lastmonth_tasks, 
            'session' : request.session })

def single(request, task_id):
    """
    Shows a single Task.
    """
    task = Task.objects.get(pk=task_id)
    form = EditTaskForm(instance=task)
    return render_to_response('tasks_single.html',
        {   'media_url': MEDIA_URL,
            'task': task, 
            'form': form,
            'session' : request.session })

def list_done(request):
    """
    List only already done Tasks.
    """
    return main(request, True)

def tags_filter(request, tag_name):
    """
    Filter Tasks by Tag.
    """
    from lucuma.tags.models import Tag
    
    tag = Tag.objects.filter(name__exact=tag_name)
    if len(tag) == 1:
        tasks = tag[0].task_set.all()
        return render_to_response('tasks_tags_list.html',
            {   'media_url': MEDIA_URL,
                'tag_tasks': tasks,
                'tag_name': tag_name,
                'session' : request.session })
    else:
        return main(request)
